/****************************************************************************
  @file    : tvbox_86.c
  @date    : 2020-07-25
  @brief   :
*****************************************************************************/
/* Includes ----------------------------------------------------------------*/
#include "remote.h"
#include "string.h"
#include "tvbox.h"

/* Private typedef ---------------------------------------------------------*/
/* Private define ----------------------------------------------------------*/
//ʱ�䶨��
#define  BOX_LEADCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)2700 )
#define  BOX_LEADCODE_TIME_L		(uint16_t)( (~0x8000)& (uint16_t)888  )
//#define  BOX_ENDCODE_TIME_H			(uint16_t)( 0x8000   | (uint16_t)560  )
#define  BOX_ENDCODE_TIME_L	    	(uint16_t)( (~0x8000)& (uint16_t)25000)

#define  BOX_T0_TIME_H				(uint16_t)( (~0x8000)& (uint16_t)889  )
#define  BOX_T0_TIME_L				(uint16_t)( 0x8000   | (uint16_t)889  )
#define  BOX_T1_TIME_H				(uint16_t)( 0x8000   | (uint16_t)889  )
#define  BOX_T1_TIME_L				(uint16_t)( (~0x8000)& (uint16_t)889  )

#define  BOX_BIT0_TIME_H			(uint16_t)( (~0x8000)& (uint16_t)444  )
#define  BOX_BIT0_TIME_L			(uint16_t)( 0x8000   | (uint16_t)444  )
#define  BOX_BIT1_TIME_H			(uint16_t)( 0x8000   | (uint16_t)444  )
#define  BOX_BIT1_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)444  )

//��ֵ����
static const uint8_t BOX_Code[TVBOX_KEY_NUM] = {
 0xFF ,//0x01����չ-���أ�
 0xFF ,//0x02����չ-AV/TV��
 0xFF ,//0x03����չ-����+��
 0xFF ,//0x04����չ-����-��
 0xFF ,//0x05����չ-Ƶ��+��
 0xFF ,//0x06����չ-Ƶ��-��
 0xFF ,//0x07����չ-������
 0xFF ,//0x08����չ-������
 0xFF ,//0x09����չ-������
 0xFF ,//0x0A����չ-������
 0x0C ,//0x0B�����أ�
 0x40 ,//0x0C���˵���
 0x0F ,//0x0D����Ϣ��
 0x0A ,//0x0E�����أ�
 0x0D ,//0x0F��������
 0xFF ,//0x10��������
 0x10 ,//0x11������+��
 0x11 ,//0x12������-��
 0x20 ,//0x13��Ƶ��+��
 0x21 ,//0x14��Ƶ��-��
 0x58 ,//0x15���ϣ�
 0x59 ,//0x16���£�
 0x5A ,//0x17����
 0x5B ,//0x18���ң�
 0x5C ,//0x19��ȷ����
 0x38 ,//0x1A���ź�Դ��
 0x54 ,//0x1B����ҳ��
 0xFF ,//0x1C��������
 0xFF ,//0x1D��������
 0x00 ,//0x1E������0��
 0x01 ,//0x1F������1��
 0x02 ,//0x20������2��
 0x03 ,//0x21������3��
 0x04 ,//0x22������4��
 0x05 ,//0x23������5��
 0x06 ,//0x24������6��
 0x07 ,//0x25������7��
 0x08 ,//0x26������8��
 0x09 ,//0x27������9��
 0xFF ,//0x28�����뷨��
};

/* Private macro -----------------------------------------------------------*/
/* Private variables -------------------------------------------------------*/
static uint16_t BoxSendlen = 0;
static uint8_t BitMask = 0;

/* Private function prototypes ---------------------------------------------*/
static void PackageByte(uint8_t byte);

/* Private functions -------------------------------------------------------*/
static void PackageByte(uint8_t byte)
{
	uint8_t i;

	for(i = 0; i<8; i++) {
		if(byte&(0x80>>(i))) {	//�ߵ�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
		} else {				//�͵�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		}
	}
}

/* Public functions --------------------------------------------------------*/
void TVBox86IrPackage(void *p)
{
	uint8_t  i,p_byte = 0xFF;

	uint8_t codeNum = *(uint8_t*)p;

	if(codeNum == 0 || codeNum >= TVBOX_KEY_NUM) return ;

	p_byte = BOX_Code[codeNum-1];

	if(p_byte == 0xFF) return;

	//�ȴ��������
	//while(IrDev.State != IRREMOTE_IDLE);
	memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));

	BoxSendlen = 0;

	//Lead code
	IrSendBuf[BoxSendlen++] = BOX_LEADCODE_TIME_H;
	IrSendBuf[BoxSendlen++] = BOX_LEADCODE_TIME_L;

	//S
	IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
	IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;

	//S
	IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
	IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
	IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
	IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
	IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
	IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;

	// toggle
	++ BitMask;

	if(BitMask&0x01) {
		IrSendBuf[BoxSendlen++] = BOX_T1_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_T1_TIME_L;
	} else {
		IrSendBuf[BoxSendlen++] = BOX_T0_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_T0_TIME_L;
	}

	//���ÿ�����
	PackageByte(0x00);
	PackageByte(p_byte);

	//���ý�����
	IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
	IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
	IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
	IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
	
	//�������ύ
	IrDev.Ptr 		= IrSendBuf;
	IrDev.Len 		= BoxSendlen;
	IrDev.State 	= IRREMOTE_REQUIRE;
}
